home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / watcom / w_modey / test.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-21  |  14.5 KB  |  486 lines

  1. #include <conio.h>
  2. #include <dos.h>
  3. #include <mem.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7.  
  8. #include "ytypes.hpp"
  9. #include "yprim.hpp"
  10. #include "ypal.hpp"
  11. #include "yblitbuf.hpp"
  12. #include "fixed32.hpp"
  13.  
  14. #define MAXLINE 100000L
  15.  
  16. #define BUDDHA_DEMO 1
  17. #define FIRE_DEMO 1
  18. #define MONSTER_DEMO_ONE 1
  19. #define MONSTER_DEMO_TWO 1
  20.  
  21.  
  22. int gbuddha;
  23.  
  24. int
  25. buddha_demo(void)
  26. {
  27.     clock_t begin, end;
  28.     Iangle theta1, theta2;
  29.     Fixed32 trigSin, trigCos;
  30.     long count, y;
  31.     blitbuf blit_image, sprite_image;
  32.     BYTE *ptr1;
  33.     BYTE *ptr2;
  34.  
  35.     clear(0);
  36.     load_blitbufPCX("buddha.pcx", &sprite_image);
  37.     scale_blitbuf(152, 168, &sprite_image);
  38.     alloc_blitbuf(&blit_image, 152, 168);
  39.  
  40.     // 152x168 image
  41.     bitblit(84, 16, &sprite_image);
  42.     getch();
  43.  
  44.     initFixed32();
  45.  
  46.     theta1=0;
  47.     count=0;
  48.  
  49.     begin = clock();
  50.  
  51.     while (!kbhit()) {
  52.         ptr1 = sprite_image.image;
  53.         ptr2 = blit_image.image;
  54.         theta2 = theta1;
  55.  
  56.         y=168;
  57.         while (y--) {
  58.             CosSin(theta2, &trigCos, &trigSin);
  59.             scale_scanline(ptr1, ptr2, 152, 152, (trigCos >> 10) + 88);
  60. #if 1
  61.             memcpy(ptr2+152, ptr2, 152);
  62.             ptr1 += 304;
  63.             ptr2 += 304;
  64.             theta2 += 4;
  65.             y--;
  66. #else
  67.             ptr1 += 152;
  68.             ptr2 += 152;
  69.             theta2 += 2;
  70. #endif
  71.         }
  72.  
  73.         theta1 += 2;
  74.  
  75.         bitblit(84, 16, &blit_image);
  76.         count++;
  77.     }
  78.  
  79.     end = clock();
  80.  
  81.     getch();
  82.  
  83.     clear_blitbuf(&blit_image);
  84.     clear_blitbuf(&sprite_image);
  85.  
  86.     gbuddha = count;
  87.  
  88.     return (end - begin);
  89. }
  90.  
  91.  
  92. void
  93. fire_demo(void)
  94. {
  95.     #define V_WIDTH         80
  96.     #define V_HEIGHT        50
  97.     #define BUF_WIDTH       80
  98.     #define BUF_HEIGHT      56
  99.     #define REWIND          (320 * 3)
  100.     #define BUF_SIZE        (BUF_WIDTH * BUF_HEIGHT)
  101.  
  102. unsigned char fire_pal[768] = {
  103.       0,   0,   0,       0,   0,  24,       0,   0,  24,       0,   0,  28,
  104.       0,   0,  32,       0,   0,  32,       0,   0,  36,       0,   0,  40,
  105.       8,   0,  40,      16,   0,  36,      24,   0,  36,      32,   0,  32,
  106.      40,   0,  28,      48,   0,  28,      56,   0,  24,      64,   0,  20,
  107.      72,   0,  20,      80,   0,  16,      88,   0,  16,      96,   0,  12,
  108.     104,   0,   8,     112,   0,   8,     120,   0,   4,     128,   0,   0,
  109.     128,   0,   0,     132,   0,   0,     136,   0,   0,     140,   0,   0,
  110.     144,   0,   0,     144,   0,   0,     148,   0,   0,     152,   0,   0,
  111.     156,   0,   0,     160,   0,   0,     160,   0,   0,     164,   0,   0,
  112.     168,   0,   0,     172,   0,   0,     176,   0,   0,     180,   0,   0,
  113.     184,   4,   0,     188,   4,   0,     192,   8,   0,     196,   8,   0,
  114.     200,  12,   0,     204,  12,   0,     208,  16,   0,     212,  16,   0,
  115.     216,  20,   0,     220,  20,   0,     224,  24,   0,     228,  24,   0,
  116.     232,  28,   0,     236,  28,   0,     240,  32,   0,     244,  32,   0,
  117.     252,  36,   0,     252,  36,   0,     252,  40,   0,     252,  40,   0,
  118.     252,  44,   0,     252,  44,   0,     252,  48,   0,     252,  48,   0,
  119.     252,  52,   0,     252,  52,   0,     252,  56,   0,     252,  56,   0,
  120.     252,  60,   0,     252,  60,   0,     252,  64,   0,     252,  64,   0,
  121.     252,  68,   0,     252,  68,   0,     252,  72,   0,     252,  72,   0,
  122.     252,  76,   0,     252,  76,   0,     252,  80,   0,     252,  80,   0,
  123.     252,  84,   0,     252,  84,   0,     252,  88,   0,     252,  88,   0,
  124.     252,  92,   0,     252,  96,   0,     252,  96,   0,     252, 100,   0,
  125.     252, 100,   0,     252, 104,   0,     252, 104,   0,     252, 108,   0,
  126.     252, 108,   0,     252, 112,   0,     252, 112,   0,     252, 116,   0,
  127.     252, 116,   0,     252, 120,   0,     252, 120,   0,     252, 124,   0,
  128.     252, 124,   0,     252, 128,   0,     252, 128,   0,     252, 132,   0,
  129.     252, 132,   0,     252, 136,   0,     252, 136,   0,     252, 140,   0,
  130.     252, 140,   0,     252, 144,   0,     252, 144,   0,     252, 148,   0,
  131.     252, 152,   0,     252, 152,   0,     252, 156,   0,     252, 156,   0,
  132.     252, 160,   0,     252, 160,   0,     252, 164,   0,     252, 164,   0,
  133.     252, 168,   0,     252, 168,   0,     252, 172,   0,     252, 172,   0,
  134.     252, 176,   0,     252, 176,   0,     252, 180,   0,     252, 180,   0,
  135.     252, 184,   0,     252, 184,   0,     252, 188,   0,     252, 188,   0,
  136.     252, 192,   0,     252, 192,   0,     252, 196,   0,     252, 196,   0,
  137.     252, 200,   0,     252, 200,   0,     252, 204,   0,     252, 208,   0,
  138.     252, 208,   0,     252, 208,   0,     252, 208,   0,     252, 208,   0,
  139.     252, 212,   0,     252, 212,   0,     252, 212,   0,     252, 212,   0,
  140.     252, 216,   0,     252, 216,   0,     252, 216,   0,     252, 216,   0,
  141.     252, 216,   0,     252, 220,   0,     252, 220,   0,     252, 220,   0,
  142.     252, 220,   0,     252, 224,   0,     252, 224,   0,     252, 224,   0,
  143.     252, 224,   0,     252, 228,   0,     252, 228,   0,     252, 228,   0,
  144.     252, 228,   0,     252, 228,   0,     252, 232,   0,     252, 232,   0,
  145.     252, 232,   0,     252, 232,   0,     252, 236,   0,     252, 236,   0,
  146.     252, 236,   0,     252, 236,   0,     252, 240,   0,     252, 240,   0,
  147.     252, 240,   0,     252, 240,   0,     252, 240,   0,     252, 244,   0,
  148.     252, 244,   0,     252, 244,   0,     252, 244,   0,     252, 248,   0,
  149.     252, 248,   0,     252, 248,   0,     252, 248,   0,     252, 252,   0,
  150.     252, 252,   4,     252, 252,   8,     252, 252,  12,     252, 252,  16,
  151.     252, 252,  20,     252, 252,  24,     252, 252,  28,     252, 252,  32,
  152.     252, 252,  36,     252, 252,  40,     252, 252,  40,     252, 252,  44,
  153.     252, 252,  48,     252, 252,  52,     252, 252,  56,     252, 252,  60,
  154.     252, 252,  64,     252, 252,  68,     252, 252,  72,     252, 252,  76,
  155.     252, 252,  80,     252, 252,  84,     252, 252,  84,     252, 252,  88,
  156.     252, 252,  92,     252, 252,  96,     252, 252, 100,     252, 252, 104,
  157.     252, 252, 108,     252, 252, 112,     252, 252, 116,     252, 252, 120,
  158.     252, 252, 124,     252, 252, 124,     252, 252, 128,     252, 252, 132,
  159.     252, 252, 136,     252, 252, 140,     252, 252, 144,     252, 252, 148,
  160.     252, 252, 152,     252, 252, 156,     252, 252, 160,     252, 252, 164,
  161.     252, 252, 168,     252, 252, 168,     252, 252, 172,     252, 252, 176,
  162.     252, 252, 180,     252, 252, 184,     252, 252, 188,     252, 252, 192,
  163.     252, 252, 196,     252, 252, 200,     252, 252, 204,     252, 252, 208,
  164.     252, 252, 208,     252, 252, 212,     252, 252, 216,     252, 252, 220,
  165.     252, 252, 224,     252, 252, 228,     252, 252, 232,     252, 252, 236,
  166.     252, 252, 240,     252, 252, 244,     252, 252, 248,     252, 252, 252
  167. };
  168.  
  169.     unsigned char *screen;
  170.     unsigned char *flamebuf_ptr;
  171.     unsigned char *flamebuf;
  172.     unsigned char temp;
  173.     unsigned short int i, j, skip;
  174.     unsigned char temp_byte;
  175.     unsigned char line_buf[320];
  176.     unsigned char *line_buf_ptr;
  177.  
  178.     set_palette(fire_pal);
  179.     flamebuf = new unsigned char[BUF_SIZE];
  180.     skip   = 320 - 3;
  181.  
  182.     // Initialize the video buffer to 0's
  183.     memset(flamebuf, 0, BUF_SIZE);
  184.  
  185.     while (!kbhit()) {
  186.         // Transform current buffer
  187.         flamebuf_ptr = flamebuf;
  188.         i = (BUF_HEIGHT - 2);
  189.         while (i--) {
  190.             *flamebuf_ptr = (*(flamebuf_ptr + BUF_WIDTH) +
  191.                        *(flamebuf_ptr + (BUF_WIDTH - 1)) +
  192.                        *(flamebuf_ptr + (BUF_WIDTH + 1)) +
  193.                        *(flamebuf_ptr + (BUF_WIDTH * 2))) >> 2;
  194.  
  195.             flamebuf_ptr += BUF_WIDTH;
  196.             temp = *flamebuf_ptr;
  197.             if (temp > 11) {
  198.                 *flamebuf_ptr -= 12;
  199.             } else if (temp > 3) {
  200.                 *flamebuf_ptr -= 4;
  201.             } else {
  202.                 *flamebuf_ptr = 0;
  203.             }
  204.             flamebuf_ptr += (1 - BUF_WIDTH);
  205.  
  206.             j = (BUF_WIDTH - 2);
  207.             while (j--) {
  208.                 *flamebuf_ptr = (*(flamebuf_ptr + BUF_WIDTH) +
  209.                            *(flamebuf_ptr + (BUF_WIDTH - 1)) +
  210.                            *(flamebuf_ptr + (BUF_WIDTH + 1)) +
  211.                            *(flamebuf_ptr + (BUF_WIDTH * 2))) >> 2;
  212.  
  213.                 flamebuf_ptr += BUF_WIDTH;
  214.                 temp = *flamebuf_ptr;
  215.                 if (temp > 11) {
  216.                     *flamebuf_ptr -= 12;
  217.                 } else if (temp > 3) {
  218.                     *flamebuf_ptr -= 4;
  219.                 } else {
  220.                     *flamebuf_ptr = 0;
  221.                 }
  222.                 flamebuf_ptr += (1 - BUF_WIDTH);
  223.             }
  224.  
  225.             *flamebuf_ptr = (*(flamebuf_ptr + BUF_WIDTH) +
  226.                        *(flamebuf_ptr + (BUF_WIDTH - 1)) +
  227.                        *(flamebuf_ptr + (BUF_WIDTH * 2)) +
  228.                        *(flamebuf_ptr + (BUF_WIDTH * 2) +
  229.                                         (BUF_WIDTH - 1))) >> 2;
  230.  
  231.             flamebuf_ptr += BUF_WIDTH;
  232.             temp = *flamebuf_ptr;
  233.             if (temp > 11) {
  234.                 *flamebuf_ptr -= 12;
  235.             } else if (temp > 3) {
  236.                 *flamebuf_ptr -= 4;
  237.             } else {
  238.                 *flamebuf_ptr = 0;
  239.             }
  240.             flamebuf_ptr += (1 - BUF_WIDTH);
  241.         }
  242.  
  243.         // Set new bottom line with random white or black
  244.         temp = 0;
  245.         flamebuf_ptr = flamebuf + (BUF_WIDTH * (BUF_HEIGHT - 2));
  246.  
  247.         j = BUF_WIDTH;
  248.         temp = 0;
  249.         while (j--) {
  250.             // We change the value 1/4 of the time
  251.             if ((rand() & 0x03) == 3) {
  252.                 temp = (255 - temp);
  253.             }
  254.  
  255.             *(flamebuf_ptr + BUF_WIDTH) = temp;
  256.             *flamebuf_ptr++             = temp;
  257.         }
  258.  
  259.         // Write the buffer to the screen
  260.         screen = RowsY[0];
  261.         flamebuf_ptr = flamebuf;
  262. //        wait_for_retrace();
  263.         i = V_HEIGHT;
  264.         while (i--) {
  265.             j = V_WIDTH;
  266.             line_buf_ptr = line_buf;
  267.             while (j--) {
  268.                 temp_byte = *flamebuf_ptr++;
  269.                 *line_buf_ptr++ = temp_byte;
  270.                 *line_buf_ptr++ = temp_byte;
  271.                 *line_buf_ptr++ = temp_byte;
  272.                 *line_buf_ptr++ = temp_byte;
  273.             }
  274.  
  275.             memcpy(screen, line_buf, 320);
  276.             screen += 320;
  277.             memcpy(screen, line_buf, 320);
  278.             screen += 320;
  279.             memcpy(screen, line_buf, 320);
  280.             screen += 320;
  281.             memcpy(screen, line_buf, 320);
  282.             screen += 320;
  283.         }
  284.     }
  285.  
  286.     getch();
  287.  
  288.     delete flamebuf;
  289. }
  290.  
  291.  
  292. int
  293. monster_demo1(void)
  294. {
  295.     clock_t begin, end;
  296.     short int i;
  297.     blitbuf sprite_image, blit_image;
  298.  
  299.     clear(0);
  300.  
  301.     load_blitbufPCX("monster.pcx", &sprite_image);
  302.     bitblit(0, 0, &sprite_image);
  303.     getch();
  304.  
  305.     blit_image.image = NULL;
  306.  
  307.     i=99;
  308.  
  309.     begin = clock();
  310.  
  311.     while (i > 0) {
  312.         scale_blitbuf(120 + (i << 1), (i << 1), &sprite_image, &blit_image);
  313.         box((99 - i), (99 - i), (220 + i), (100 + i), 0);
  314.         bitblit(100 - i, 100 - i, &blit_image);
  315.         i--;
  316.     }
  317.  
  318.     end = clock();
  319.  
  320.     i=120;
  321.     while (i > 0) {
  322.         scale_blitbuf(i, 2, &sprite_image, &blit_image);
  323.  
  324.         putpixel(159 - (i >> 1),  99, 0);
  325.         putpixel(159 - (i >> 1), 100, 0);
  326.         putpixel(160 + (i >> 1),  99, 0);
  327.         putpixel(160 + (i >> 1), 100, 0);
  328.  
  329.         bitblit(160 - (i >> 1), 99, &blit_image);
  330.         delay(5);
  331.         i -= 2;
  332.     }
  333.  
  334.     filledbox(156, 99, 163, 100, 0);
  335.     putpixel(159, 100, 1);
  336.     delay(250);
  337.     putpixel(159, 100, 0);
  338.  
  339.     filledbox(156, 99, 163, 120, 0);
  340.     clear_blitbuf(&sprite_image);
  341.     clear_blitbuf(&blit_image);
  342.  
  343.     getch();
  344.  
  345.     return (end - begin);
  346. }
  347.  
  348.  
  349. void
  350. monster_demo2(void)
  351. {
  352.     short int i;
  353.     blitbuf sprite_image, blit_image;
  354.  
  355.     clear(0);
  356.  
  357.     load_blitbufPCX("monster.pcx", &sprite_image);
  358.     bitblit(0, 0, &sprite_image);
  359.     getch();
  360.  
  361.     blit_image.image = NULL;
  362.  
  363.     i=99;
  364.     while (i) {
  365.         vertical_scale_blitbuf((i << 1), &sprite_image, &blit_image);
  366.         box(0, (99 - i), 319, (100 + i), 0);
  367.         bitblit(0, 100 - i, &blit_image);
  368.         i--;
  369.     }
  370.  
  371.     filledbox(0, 100, 319, 100, 0);
  372.  
  373.     i=318;
  374.     while (i > 0) {
  375.         scale_blitbuf(i, 1, &blit_image, &sprite_image);
  376.  
  377.         putpixel(159 - (i >> 1),  99, 0);
  378.         putpixel(159 - (i >> 1), 100, 0);
  379.         putpixel(160 + (i >> 1),  99, 0);
  380.         putpixel(160 + (i >> 1), 100, 0);
  381.  
  382.         bitblit(160 - (i >> 1), 99, &sprite_image);
  383.         delay(5);
  384.         i -= 2;
  385.     }
  386.  
  387.     filledbox(156, 99, 163, 100, 0);
  388.     putpixel(159, 100, 1);
  389.     delay(250);
  390.     putpixel(159, 100, 0);
  391.     filledbox(156, 99, 163, 100, 0);
  392.     clear_blitbuf(&sprite_image);
  393.     clear_blitbuf(&blit_image);
  394.  
  395.     getch();
  396. }
  397.  
  398.  
  399. void
  400. main(void)
  401. {
  402.     clock_t begin, end;
  403.     long i, j, x1, x2, y1, y2, mtime, btime;
  404.     long time1, time2;
  405.  
  406.     set320x200x256();
  407.  
  408.     loadfont("vga8x8.fnt");
  409.  
  410.     for (i=0; i < 320; i++) {
  411.         for (j=0; j < 200; j++) {
  412.             putpixel(i, j, (i & 0xFF));
  413.         }
  414.     }
  415.  
  416.     getch();
  417.  
  418.     clear(0);
  419.  
  420.     begin = clock();
  421.  
  422.     for (i=0; i < MAXLINE; i++) {
  423.         x1 = rand() % 320;
  424.         x2 = rand() % 320;
  425.         y1 = rand() % 200;
  426.         y2 = rand() % 200;
  427.  
  428.         line(x1, y1, x2, y2, (i & 0xFF));
  429.     }
  430.  
  431.     end = clock();
  432.     time1 = (end - begin);
  433.  
  434.     getch();
  435.  
  436.     begin = clock();
  437.  
  438.     for (i=0; i < MAXLINE; i++) {
  439.         x1 = rand() % 320;
  440.         x2 = rand() % 320;
  441.         y1 = rand() % 200;
  442.         y2 = rand() % 200;
  443.  
  444.         x1 = (i & 0xFF);
  445.     }
  446.  
  447.     end = clock();
  448.     time2 = (end - begin);
  449.  
  450. #if BUDDHA_DEMO
  451.     clear(0);
  452.     btime = buddha_demo();
  453. #endif
  454.  
  455. #if FIRE_DEMO
  456.     clear(0);
  457.     fire_demo();
  458. #endif
  459.  
  460. #if MONSTER_DEMO_ONE
  461.     mtime = monster_demo1();
  462. #endif
  463.  
  464. #if MONSTER_DEMO_TWO
  465.     monster_demo2();
  466. #endif
  467.  
  468.     putstring(0,0,"BYE!", 1);
  469.  
  470.     getch();
  471.  
  472.     set80x25();
  473.  
  474.     printf("%ld lines took %4d ticks\n", MAXLINE, time1);
  475.     printf("rand() overhead = %4d ticks\n", time2);
  476.     printf("Time in lineY   = %4d ticks\n", (time1 - time2));
  477.     printf("%d lines per second!\n\n", (MAXLINE * 1000) /
  478.                                        (55 * (time1 - time2)));
  479.  
  480.     printf("Buddha = %4d blits\n", gbuddha);
  481.     printf("       = %4d per second\n", (gbuddha * 1000) / (55 * btime));
  482.     printf("Buddha = %4d scanline stretches\n", (gbuddha * 168));
  483.     printf("       = %4d per second!\n\n", (gbuddha * 168000) / (55 * btime));
  484. }
  485.  
  486.